//
//  HXDLoginViewController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/23.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDLoginViewController.h"
#import "AppDelegate.h"
#import "UMSocial.h"
#import "MBProgressHUD+MJ.h"
#import "HXDPersonalCentreViewController.h"

@interface HXDLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *rememberPasswordButton;
@property (weak, nonatomic) IBOutlet UIView *userNameView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineWide;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdButton;
@property (assign, nonatomic) BOOL isLogin;

@end

@implementation HXDLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"登录";
    self.userNameView.layer.cornerRadius = 5;
    self.userNameView.layer.masksToBounds = YES;
    self.passwordView.layer.cornerRadius = 5;
    self.passwordView.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 5;
    self.loginButton.layer.masksToBounds = YES;
    self.lineWide.constant = self.forgetPwdButton.frame.size.width;
    [self.userNameText setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.passwordText setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userNameText addTarget:self action:@selector(inputText) forControlEvents:UIControlEventEditingChanged];
    [self.passwordText addTarget:self action:@selector(inputText) forControlEvents:UIControlEventEditingChanged];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orLogin) name:@"login" object:nil];
}

- (void)orLogin {
    self.isLogin = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.userNameText.text = [kSaveData objectForKey:kAccount];
    self.rememberPasswordButton.selected = [kSaveData boolForKey:kRmbPassword];
    if (self.rememberPasswordButton.selected) {
        self.passwordText.text = [kSaveData objectForKey:kPassword];
    }
    [self inputText];
    [self loginAction];
}

- (void)inputText {
    self.loginButton.enabled = self.userNameText.text.length && self.passwordText.text.length;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.userNameText resignFirstResponder];
    [self.passwordText resignFirstResponder];
}

- (IBAction)rmbPwdAction {
    self.rememberPasswordButton.selected = !self.rememberPasswordButton.selected;
//    if (self.rememberPasswordButton.selected == YES) {
//        self.rememberPasswordButton.selected = NO;
//    } else {
//        self.rememberPasswordButton.selected = YES;
//    }
}

- (IBAction)loginAction {
    NSString *pattern = @"^[0-9a-zA-Z]{6,12}$";
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    NSArray *resultArray = [regularExpression matchesInString:self.passwordText.text options:1 range:NSMakeRange(0, self.passwordText.text.length)];
    if (resultArray.count == 0) {
        [MBProgressHUD showError:@"密码输入不合法，请重新输入！"];
    } else {
        [MBProgressHUD showMessage:@"正在登录..."];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            if ([self.userNameText.text isEqualToString:@"hxd"] && [self.passwordText.text isEqualToString:@"19920216hxd"]) {
                self.isLogin = YES;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"isLogin" object:@(self.isLogin)];
                self.rightButton.frame = CGRectMake(kUIScreenWidth - 2 * kNavigationButtonWidth + 30, kUIStatusBarHeight, kNavigationButtonWidth, kNavigationButtonWidth);
                [self.rightButton setTitle:nil forState:UIControlStateNormal];
                UIImage *iconImage = [UIImage imageWithData:[kSaveData objectForKey:kIconImage]];
                [self.rightButton setBackgroundImage:iconImage forState:UIControlStateNormal];
                self.rightButton.layer.cornerRadius = 20;
                self.rightButton.clipsToBounds = YES;
                [kSaveData setObject:self.userNameText.text forKey:kAccount];
                [kSaveData setObject:self.passwordText.text forKey:kPassword];
                [kSaveData setBool:self.rememberPasswordButton.selected forKey:kRmbPassword];
                [MBProgressHUD showSuccess:@"登录成功！"];
            } else
                [MBProgressHUD showError:@"账户名或密码错误！"];
        });
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)thirdLogin:(UIButton *)sender {
    //友盟分享
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5657328767e58ea562003340"
                                      shareText:@"大耳朵图图"
                                     shareImage:[UIImage imageNamed:@"tutu"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ, UMShareToDouban, UMShareToEmail, UMShareToFacebook,nil]
                                       delegate:nil];
}

- (void)backMethod {
    [self.navigationController popViewControllerAnimated:YES];
    //创建AppDelegate的代理对象
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.tabBar isHiddenTabBar:NO];
}

- (void)enterMethod {
    HXDPersonalCentreViewController *personalCentreViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HXDPersonalCentreViewController"];
    [self.navigationController pushViewController:personalCentreViewController animated:YES];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.tabBar isHiddenTabBar:NO];
}

@end
